// subIndex/pages_first/identity/identity.js
const appg = getApp().globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: getApp().globalData.userInfo.id,
    school_ID: null,
    loading: false,
    identityInfo: null,
    identityList: null,
    show: false,
    showClass: false,
    loading2: false,
    columns: [],
    jsonColumns: [],
    jsonRows: [],
    rows: [],
    class_ID: null,
    class_name: null,
    student_id: null,
    show_tea: false,
    teacher_id: null,
    position_name: null,
    position_ID: null,
    applyId: null,
    applyName: null,
    showApply: false,
    applyList: [],
    jsonApply: []
  },
  // 获取身份列表
  async getIdentityList() {
    let uid = this.data.uid;
    this.setData({
      loading: true
    })
    try {
      let json = await getApp().$axios("/getIdentityList?id=" + uid, "GET")
      this.setData({
        loading: false
      })
      if (json.status !== 200) {
        wx.showToast({
          title: json.msg,
          icon: 'error'
        })
        return
      }
      this.computeNoIdentity(json.data)
      this.setData({
        identityList: json.data,
        identityInfo: json.identityInfo
      })
      // 判断是否点击了申请学生身份
      this.judge()
    } catch (error) {
      wx.hideLoading()
      if (error.status && error.status == 500) {
        wx.showModal({
          title: '提示',
          content: 'token信息已过期，重新登录',
          showCancel: false,
          complete: (res) => {
            wx.clearStorageSync()
          }
        })
        return
      }
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  // 计算为拥有的身份
  computeNoIdentity(data) {
    let arr = [1, 2, 3]
    const diffArr = arr.filter(num => {
      return !data.some(item => item.identity_id == num)
    });
    console.log(diffArr);
    this.setData({
      noIdentity: diffArr
    })
  },
  // 申请学生身份
  approval_stu() {
    wx.navigateTo({
      url: '/subpacks/pages_first/change_face/change_face',
    })
  },
  // 申请老师身份
  approval_tea() {
    this.setData({
      show_tea: true
    })
  },
  // 判断是否点击了申请学生身份
  async judge() {
    // 如果有学生身份直接忽略
    /**
     * 返回true说明已经是或者有学生身份
     */
    console.log(this.data.identityList);
    const bool = this.data.identityList.some((item) => {
      return item.identity_id == 1
    })
    if (bool) {
      return
    }
    // 判断是否录入人脸 录入了就可以弹出去填写信息框
    try {
      const {
        data,
        status
      } = await getApp().$axios("/checkFace?id=" + this.data.uid, 'GET')
      if (status !== 200) {
        wx.showToast({
          title: '查询出错了',
          icon: 'none'
        })
        return
      }
      if (data) {
        this.setData({
          show: true
        })
      } else {
        this.setData({
          show: false
        })
      }
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }

  },
  onClose() {
    this.setData({
      show: false
    })
  },
  // 关闭班级弹出窗
  onCloseClass() {
    this.setData({
      showClass: false
    })
  },
  // 打开选择班级的弹出窗
  async show_picker_class() {
    this.setData({
      showClass: true,
      loading2: true
    })
    try {
      let json = await getApp().$axios("/getClassList?id=" + this.data.identityInfo.college_ID, "GET");
      console.log(json);
      if (json.status !== 200) {
        wx.showToast({
          title: json.data,
          icon: 'error'
        });
        return
      }
      const columns = []
      json.data.forEach(item => {
        columns.push(item.class_name)
      });
      this.setData({
        jsonColumns: json.data,
        loading2: false,
        columns: columns
      })
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  onConfirm(e) {
    let {
      index,
      value
    } = e.detail;
    this.setData({
      class_name: value,
      class_ID: this.data.jsonColumns[index].id
    })
    this.onCloseClass()
  },
  // 学号
  onChange(e) {
    const num = e.detail
    this.setData({
      student_id: num
    })
  },
  // 申请学生身份
  async applyStu() {
    if (this.data.student_id == null || this.data.class_ID == null) {
      wx.showToast({
        title: '请将信息填写完整',
        icon: 'none'
      })
      return
    }
    const params = {
      uid: this.data.uid,
      student_id: this.data.student_id,
      class_ID: this.data.class_ID,
      clasa_name: this.data.class_name
    }
    const {
      data,
      status
    } = await getApp().$axios('/applyStudent', 'POST', params)
    if (status !== 200) {
      wx.showToast({
        title: '查询出错',
        icon: 'none'
      })
      return
    }
    this.setData({
      show: false
    })
    this.getIdentityList()
  },
  // 工号
  onChange_tid(e) {
    const num = e.detail
    this.setData({
      teacher_id: num
    })
  },
  // 选择职位弹窗
  async show_picker_position() {
    this.setData({
      showPosition: true,
      loading2: true
    })
    try {
      let json = await getApp().$axios("/getSchoolPosition", "GET");
      console.log(json);
      if (json.status !== 200) {
        wx.showToast({
          title: json.data,
          icon: 'error'
        });
        return
      }
      const columns = []
      json.data.forEach(item => {
        columns.push(item.position_name)
      });
      this.setData({
        jsonRows: json.data,
        loading2: false,
        rows: columns
      })
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  // 关闭职位弹窗
  onClosePosition() {
    this.setData({
      showPosition: false
    })
  },
  // 确认选中职位弹窗
  onConfirmPosition(e) {
    let {
      index,
      value
    } = e.detail;
    this.setData({
      position_name: value,
      position_ID: this.data.jsonRows[index].id
    })
    this.onClosePosition()
  },
  // 关闭老师职位申请窗口
  onClose_tea() {
    this.setData({
      show_tea: false
    })
  },
  // 选择审批人
  async show_picker_apply() {
    this.setData({
      showApply: true,
      loading2: true
    })
    try {
      let json = await getApp().$axios("/selectSchoolAdmin?id=" + this.data.school_ID, "GET");
      console.log(json);
      if (json.status !== 200) {
        wx.showToast({
          title: json.data,
          icon: 'error'
        });
        return
      }
      const columns = []
      json.data.forEach(item => {
        columns.push(item.username)
      });
      this.setData({
        jsonApply: json.data,
        loading2: false,
        applyList: columns
      })
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },
  // 关闭审批人弹窗
  onCloseApply() {
    this.setData({
      showApply: false
    })
  },
  // 确认选中审批人
  onConfirmApply(e) {
    let {
      index,
      value
    } = e.detail;
    this.setData({
      applyName: value,
      applyId: this.data.jsonApply[index].id
    })
    this.onCloseApply()
  },
  // 发起老师申请
  async applyTea() {
    wx.showLoading({
      title: '加载中',
    })
    if (this.data.teacher_id == null || this.data.position_ID == null || this.data.applyId == null) {
      wx.showToast({
        title: '请将信息填写完整',
        icon: 'none'
      })
      return
    }
    try {
      // 检验之前是否申请过
      const res = await getApp().$axios("/checkTeacherApply?uid=" + this.data.uid, "GET")
      if (res.status !== 200) {
        wx.showToast({
          title: json.data,
          icon: 'none'
        })
        return
      }
      if (!res.data) {
        wx.showToast({
          title: '你已经申请过了，请等待审批！',
        })
        return
      } else {
        console.log("hhghhgg");
        const params = {
          uid: this.data.uid,
          teacher_id: this.data.teacher_id,
          position_ID: this.data.position_ID,
          position_name: this.data.position_name,
          applyId: this.data.applyId
        }
        const {
          data,
          status
        } = await getApp().$axios('/applyTeacher', 'POST', params)
        wx.hideLoading()
        if (status !== 200) {
          wx.showToast({
            title: json.data,
            icon: 'none'
          })
          return
        }
        this.setData({
          show_tea: false
        })
        wx.showModal({
          title: '提示',
          content: json.data,
          showCancel: false,
          complete: (res) => {

          }
        })
      }
    } catch (error) {
      wx.showToast({
        title: error.msg,
        icon: 'error'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      school_ID: appg.userInfo.school_ID,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getIdentityList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})